package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.*;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.tomcat.websocket.WsSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentFeeValueService feeValueService;
    @Autowired
    private ApartmentFacilityService facilityService;
    @Autowired
    private ApartmentLabelService labelService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //新增一条记录
        super.saveOrUpdate(apartmentSubmitVo);
        //apartmentSubmitVo不为空的情况下
        boolean flag = apartmentSubmitVo.getId() != null;
        //如果是修改操作，则将之前的关系删除
        if (flag) {
            feeValueService.removeBatchByIds(apartmentSubmitVo.getFeeValueIds());
            facilityService.removeBatchByIds(apartmentSubmitVo.getFacilityInfoIds());
            labelService.removeBatchByIds(apartmentSubmitVo.getLabelIds());
            graphInfoService.remove(graphInfoService.lambdaQuery()
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, apartmentSubmitVo.getId()));
        }
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getFacilityInfoIds())) {
            facilityService.saveBatch(
                    apartmentSubmitVo.getFacilityInfoIds().stream()
                            .map(ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId())::facilityId)
                            .map(ApartmentFacility.ApartmentFacilityBuilder::build)
                            .collect(Collectors.toList()));
        }
        //3.向公寓标签关系表添加数据
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getLabelIds())) {
            labelService.saveBatch(
                    apartmentSubmitVo.getLabelIds().stream()
                            .map(ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId())::labelId)
                            .map(ApartmentLabel.ApartmentLabelBuilder::build)
                            .collect(Collectors.toList()));
        }
        //4.向公寓杂费关系表添加数据
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getFeeValueIds())) {
            feeValueService.saveBatch(
                    apartmentSubmitVo.getFeeValueIds().stream()
                            .map(ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId())::feeValueId)
                            .map(ApartmentFeeValue.ApartmentFeeValueBuilder::build)
                            .collect(Collectors.toList())
            );
        }
        //5.向图片信息表添加数据
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getGraphVoList())) {
            graphInfoService.saveBatch(
                    apartmentSubmitVo.getGraphVoList().stream()
                            .map(graphVo -> {
                                GraphInfo graphInfo = new GraphInfo();
                                BeanUtils.copyProperties(graphVo, graphInfo);
                                graphInfo.setItemType(ItemType.APARTMENT);
                                graphInfo.setItemId(apartmentSubmitVo.getId());
                                return graphInfo;
                            }).collect(Collectors.toList())
            );
        }

    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {


        return null;
    }

    @Override
    public void pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        Page<ApartmentInfo> apartmentInfoPage = lambdaQuery()
                .eq(queryVo.getCityId() != null, ApartmentInfo::getCityId, queryVo.getCityId())
                .eq(queryVo.getProvinceId() != null, ApartmentInfo::getProvinceId, queryVo.getProvinceId())
                .eq(queryVo.getDistrictId() != null, ApartmentInfo::getDistrictId, queryVo.getDistrictId())
                .page(Page.of(page.getCurrent(), page.getSize()));

        page.setPages(apartmentInfoPage.getPages());
        page.setTotal(apartmentInfoPage.getTotal());

        List<ApartmentItemVo> list = apartmentInfoPage.getRecords().stream().map(apartmentInfo -> {
            ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
            BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
            return apartmentItemVo;
        }).toList();
        List<Long> ids = apartmentInfoPage.getRecords().stream().map(BaseEntity::getId).toList();
        List<RoomStatDto> roomStatDtoList = apartmentInfoMapper.getRoomStat(ids);
        list.forEach(apartmentItemVo -> {
            apartmentItemVo.setTotalRoomCount(0L);
            apartmentItemVo.setFreeRoomCount(0L);
        });
        roomStatDtoList.forEach(roomStatDto -> {
            list.stream().filter(apartmentItemVo -> roomStatDto.getApartmentId().equals(apartmentItemVo.getId())).forEach(apartmentItemVo -> {
                apartmentItemVo.setFreeRoomCount(roomStatDto.getFreeRoomCount());
                apartmentItemVo.setTotalRoomCount(roomStatDto.getTotalRoomCount());
            });
        });

        page.setRecords(list);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfoid = this.getById(id);
        if (apartmentInfoid ==null){
            return null;
        }
        //查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphInfoById(id);

        //查询LabelInfo标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoById(id);

        //查询facilityInfoList配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityById(id);

        //查询feeValueVoList杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoBuId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //对象复制：将apartmentinfoid对象的属性赋值给apartmentDetailVo，但这要求两个对象的属性名称相同。
        BeanUtils.copyProperties(apartmentInfoid,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;

    }

}




